package com.gee.spring.boot.starter.opengemini.core.util;

import java.math.BigInteger;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;

/**
 * @author gepengjun
 * @since 2024/4/29 9:32
 */
public class TimeUtil {

    public static final BigInteger NANOSECONDS_PER_SECOND = BigInteger.valueOf(1000000000L);

    public static long timeToNanos(LocalDateTime localDateTime){
        Instant instant = localDateTime.atZone(ZoneId.systemDefault()).toInstant();
        return BigInteger.valueOf(instant.getEpochSecond())
                .multiply(BigInteger.valueOf(NANOSECONDS_PER_SECOND.longValue()))
                .add(BigInteger.valueOf(instant.getNano())).longValue();
    }

    public static LocalDateTime nanosToTime(long nanos){
        Instant instant = Instant.ofEpochSecond(nanos / NANOSECONDS_PER_SECOND.longValue(), (int) (nanos % NANOSECONDS_PER_SECOND.longValue()));
        return instant.atZone(ZoneId.systemDefault()).toLocalDateTime();
    }
}
